package com.guaoran.sso.controller;

import com.guaoran.common.annotation.Anoymous;
import com.guaoran.common.constants.MallWebConstant;
import com.guaoran.sso.controller.support.ResponseData;
import com.guaoran.sso.controller.support.ResponseEnum;
import com.guaoran.user.IUserCoreService;
import com.guaoran.user.IUserQueryService;
import com.guaoran.user.constants.ResponseCodeEnum;
import com.guaoran.user.dto.UserLoginRequest;
import com.guaoran.user.dto.UserLoginResponse;
import com.guaoran.user.dto.UserRegisterRequest;
import com.guaoran.user.dto.UserRegisterResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

/**
 * 用户登录注册模块
 */
@RestController
public class UserController extends BaseController {
    private Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    IUserCoreService userCoreService;
    @Autowired
    IUserQueryService userQueryService;


    @Autowired
    KafkaTemplate kafkaTemplate;

    /**
     * 登录，登录失败返回错误信息
     * @param username
     * @param password
     * @param response
     * @return
     */
    @Anoymous
    @PostMapping("/login")
    public ResponseData doLogin(String username, String password,
                                HttpServletResponse response) {
        ResponseData data = new ResponseData();
        UserLoginRequest request = new UserLoginRequest();
        request.setPassword(password);
        request.setUserName(username);
//        // 测试 aop
//        UserQueryRequest userQueryRequest =  new UserQueryRequest();
//        userQueryRequest.setUid(1);
//        UserQueryResponse queryResponse = userQueryService.getUserById(userQueryRequest);
//        System.out.println(queryResponse);

        //登录成功，设置用户 token
        UserLoginResponse userLoginResponse = userCoreService.login(request);
        response.addHeader("Set-Cookie",
                "access_token=" + userLoginResponse.getToken() + ";Path=/;HttpOnly");
        data.setMessage(userLoginResponse.getMsg());
        data.setCode(userLoginResponse.getCode());
        //跳转到活动页面
        data.setData(MallWebConstant.MALL_ACTIVITY_ACCESS_URL);
        return data;
    }

    /**
     * 注册
     * @param username
     * @param password
     * @param mobile
     * @return
     */
    @PostMapping("/register")
    @Anoymous
    public @ResponseBody
    ResponseData register(String username, String password, String mobile) {
        ResponseData data = new ResponseData();

        UserRegisterRequest request = new UserRegisterRequest();
        request.setMobile(mobile);
        request.setUsername(username);
        request.setPassword(password);
        try {
            UserRegisterResponse response = userCoreService.register(request);
            if(response.getCode().equals(ResponseCodeEnum.SUCCESS.getCode())){
                //异步化解耦,发送用户id，进行后续逻辑的操作，

                kafkaTemplate.send("test", response.getUserBean());
            }
            data.setMessage(response.getMsg());
            data.setCode(response.getCode());
        } catch (Exception e) {
            data.setMessage(ResponseEnum.FAILED.getMsg());
            data.setCode(ResponseEnum.FAILED.getCode());
        }
        return data;
    }

}
